package com.budwk.app.sqfw.northstation.controllers;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.sqfw.northstation.models.NsDeviceInfo;
import com.budwk.app.sqfw.northstation.service.NsdeviceinfoService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/northstation/device/info")
@ApiDefinition(tag = "NsDeviceInfo")
@Slf4j
public class NsdeviceinfoController {
    @Inject
    private NsdeviceinfoService nsDeviceInfoService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询NsDeviceInfo")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = NsDeviceInfo.class
    )
    @ApiResponses(
    implementation = NsDeviceInfo.class
    )
    public Result<?> list(@Param("..")NsDeviceInfo bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean!=null &&bean.getDeviceid() !=null && Strings.isNotBlank(bean.getDeviceid().toString())) {
            cnd.and("deviceid","=",bean.getDeviceid());
        }
        
        if (bean!=null &&bean.getName() !=null && Strings.isNotBlank(bean.getName().toString())) {
            cnd.and("name","=",bean.getName());
        }
        
        if (bean!=null &&bean.getLatitude() !=null && Strings.isNotBlank(bean.getLatitude().toString())) {
            cnd.and("latitude","=",bean.getLatitude());
        }
        
        if (bean!=null &&bean.getLongitude() !=null && Strings.isNotBlank(bean.getLongitude().toString())) {
            cnd.and("longitude","=",bean.getLongitude());
        }
        
        if (bean!=null &&bean.getType() !=null && Strings.isNotBlank(bean.getType().toString())) {
            cnd.and("type","=",bean.getType());
        }
        
        if (bean!=null &&bean.getStatus() !=null && Strings.isNotBlank(bean.getStatus().toString())) {
            cnd.and("status","=",bean.getStatus());
        }
        
        return Result.data(nsDeviceInfoService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增NsDeviceInfo")
    @ApiFormParams(
        implementation = NsDeviceInfo.class
    )
    @ApiResponses
    @SLog(tag = "新增NsDeviceInfo", value = "${args[0].id}")
    public Result<?> create(@Param("..")NsDeviceInfo nsDeviceInfo, HttpServletRequest req) {

        nsDeviceInfo.setCreatedBy(SecurityUtil.getUserId());
        nsDeviceInfoService.insert(nsDeviceInfo);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改NsDeviceInfo")
    @ApiFormParams(
        implementation = NsDeviceInfo.class
    )
    @ApiResponses
    @SLog(tag = "修改NsDeviceInfo", value = "${args[0].id}")
    public Result<?> update(@Param("..")NsDeviceInfo nsDeviceInfo, HttpServletRequest req) {
        nsDeviceInfo.setUpdatedBy(SecurityUtil.getUserId());
        nsDeviceInfoService.updateIgnoreNull(nsDeviceInfo);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取NsDeviceInfo")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        NsDeviceInfo nsDeviceInfo = nsDeviceInfoService.fetch(id);
        if (nsDeviceInfo == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(nsDeviceInfo);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除NsDeviceInfo")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除NsDeviceInfo", value = "${args[0]}")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        NsDeviceInfo nsDeviceInfo = nsDeviceInfoService.fetch(id);
        if (nsDeviceInfo == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        nsDeviceInfoService.delete(id);
        req.setAttribute("_slog_msg", nsDeviceInfo.getId());
        return Result.success();
    }
}
